package com.swotxu.mall.member.controller;

import com.swotxu.common.utils.PageUtils;
import com.swotxu.common.utils.R;
import com.swotxu.mall.member.entity.MemberEntity;
import com.swotxu.mall.member.exception.ExistException;
import com.swotxu.mall.member.exception.MemberLoginException;
import com.swotxu.mall.member.service.MemberService;
import com.swotxu.mall.member.vo.MemberLoginVo;
import com.swotxu.mall.member.vo.MemberRegistVo;
import com.swotxu.mall.member.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author swotxu
 * @email mr_xudada@163.com
 * @date 2020-12-12 18:51:41
 */
@Slf4j
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 社交登录
     * @return
     */
    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser socialUser) {
        try {
            MemberEntity entity = memberService.login(socialUser);
            return R.ok().setData(entity);
        } catch (MemberLoginException e) {
            log.warn("登陆失败！{}", e.getType());
            return R.error(e.getType());
        }
    }

    /**
     * 用户登录
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo loginVo) {
        try {
            MemberEntity entity = memberService.login(loginVo);
            return R.ok().setData(entity);
        } catch (MemberLoginException e) {
            log.warn("登陆失败！{}", e.getType());
            return R.error(e.getType());
        }
    }

    /**
     * 注册用户
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo memberRegistVo) {
        try {
            memberService.regist(memberRegistVo);
        } catch (ExistException e) {
            log.warn("注册的用户信息重复！{}", e.getType());
            return R.error(e.getType());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
